package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-09-14 16:52
 */
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {
    @Autowired
    private WmMaterialService wmMaterialService;

    @PostMapping("/upload_picture")
    //最后返回结果应该是WmMaterial
    public ResponseResult uploadPicture(MultipartFile multipartFile){

        //1.校验参数
        if(multipartFile == null || multipartFile.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传
        WmMaterial material = wmMaterialService.upload(multipartFile);

        return ResponseResult.okResult(material);
    }

    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmMaterialDto dto){
        //1.检查参数
        dto.checkParam();

        //2.查询分页数据
        return wmMaterialService.findMaterialList(dto);

    }
}
